package blog.open1111.service.impl;

import blog.open1111.entity.ArcType;
import blog.open1111.entity.Article;
import blog.open1111.entity.Link;
import blog.open1111.service.ArcTypeService;
import blog.open1111.service.ArticleService;
import blog.open1111.service.LinkService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by dengwubo on 2017/9/13.
 */
@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public void refershSystem(ServletContext application){
        System.out.println("application============="+application);
        ArcTypeService arcTypeService = (ArcTypeService)applicationContext.getBean(ArcTypeService.class);
        List<ArcType> arcTypeList = arcTypeService.list(null);
        application.setAttribute("arcTypeList",arcTypeList);

        LinkService linkService = (LinkService)applicationContext.getBean(LinkService.class);
        List<Link> linkList = linkService.list(null);
        application.setAttribute("linkList",linkList);

        ArticleService articleService = (ArticleService) applicationContext.getBean(ArticleService.class);
        List<Article> articleList = articleService.getHot();
        application.setAttribute("articleList",articleList);


    }


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application = servletContextEvent.getServletContext();
        refershSystem(application);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
